/*
 * This work is licensed under a GNU General Public License 
 * (http://www.gnu.org/copyleft/gpl.html).
 * 
 * Title: 		Mobup
 * Version: 		0.3
 * Description:         Mobile uploader for Flickr
 * Creator:             Vincenzo Lembo and Matteo Penzo
 * Source:              http://mobup.org
 *
 */


package uns.fit.gis.ui;


import javax.microedition.lcdui.*;

import uns.fit.gis.MapDrawer;
import uns.fit.gis.i18n.LocaleManager;

import ctmobup.flickr.*;
import ctmobup.utils.*;

import java.util.*;

/*
 * ConfigForm Class
 * 
 * Input form for set the Flickr parameter for uploading the images
 * It allows to set the Api Key, Secret Shared and Token
 * for the autentication of user.
 *
 */
public class ConfigForm extends Form implements CommandListener
{
    private MapDrawer midlet;
    private Command backCommand;
    private Command okCommand;
    private Flickr flickr;
    private int itemText = 0;
    
    public ConfigForm() {
        super("");
        this.midlet = MapDrawer.getMidlet();
        setCommandListener(this);
        okCommand = new Command(LocaleManager.getMessage("ok"), Command.BACK, 1);
        addCommand(okCommand);
        backCommand=new Command(LocaleManager.getMessage("back"),Command.SCREEN,3);
        addCommand(backCommand);
        append(LocaleManager.getMessage("configflickr"));
        append(new TextField(LocaleManager.getMessage("activationcode"), "", 9, TextField.NUMERIC));
        itemText = append("");
        //creating flickr object for registration
        flickr = new Flickr(MapDrawer.FlikcrApiKey, MapDrawer.FlickrSecretShared);
    }
    
    /*
     * 
     */
    public void commandAction(Command c, Displayable displayable) {
        if(c == okCommand) 
        {
            boolean reg = false;
            //display the waiting message
            ((StringItem)this.get(itemText)).setText("\n\n" + LocaleManager.getMessage("waitmsg"));
            //call the registration method to obtain the full token and store it in RecordStore
            reg = flickr.Registration(((TextField)get(1)).getString());
            
            //check for configuration error
            if(reg)
                MessageBox.Show(midlet, LocaleManager.getMessage("configuration"), LocaleManager.getMessage("configsaved"),AlertType.CONFIRMATION, 2000);
            else
                MessageBox.Show(midlet, LocaleManager.getMessage("configuration"), LocaleManager.getMessage("configfailed") + "\n"+flickr.getLastErrorMsg()+" code:"+flickr.getLastErrorCode(),AlertType.ERROR);
            midlet.showVideoCanvas();
        }
        else if(c == backCommand)
        {
            midlet.showVideoCanvas();
        }
    }
    
    public void Clear()
    {
        if(itemText!=0)
            ((StringItem)this.get(itemText)).setText("");
        ((TextField)get(1)).setString("");
    }
}


